package cc.ruih.meizanchen.mm;

import cc.ruih.meizanchen.BBLogin;
import cc.ruih.meizanchen.R;
import cc.ruih.meizanchen.Setting;
import cc.ruih.meizanchen.bb.BBIndex;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.OnGestureListener;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.ScrollView;
import android.widget.TabHost;
import android.widget.Toast;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

public class MMMain extends Activity {
	
    private TabHost tabs;
	private ImageButton footer_tab_btn1;
	private ImageButton footer_tab_btn2;
	private ImageButton footer_tab_btn3;
	private ImageButton footer_tab_btn4;
	private ImageButton footer_tab_btn5;
	
	// 好孕知识四个Tab
	private RadioButton tefy,tjjy,hljy,yyjy;
	
	private ImageView logo;	// 头部Logo
	private TextView title;		// 头部标题
	private ImageButton setBtn;		// 设置按钮
	
	private ScrollView tv1,tv2,tv3,tv4;
	
	Resources res;
	private ListView listView00, listView01, listView02, listView03, listView04;
	private ImageView fetus;
	GestureDetector detector;  // 手势检测
	int week=1;		// 第几周
	ImageButton left;		// 左
    ImageButton right;	// 右
    
    ImageButton fmzds;		// 分娩知多少按钮
    ImageButton changeUser;		// 切换至宝宝

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mm_main);
        
        logo = (ImageView) findViewById(R.id.headLogo);
        title = (TextView) findViewById(R.id.headTitle);
        // 设置按钮
        setBtn = (ImageButton) findViewById(R.id.setting);
        setBtn.setOnClickListener(buttonClick);
        
        logo.setImageResource(R.drawable.daohang1);
        title.setText("好孕知识");
        
        tefy = (RadioButton) findViewById(R.id.opt_tefy);
        tjjy = (RadioButton) findViewById(R.id.opt_tjjy);
        hljy = (RadioButton) findViewById(R.id.opt_hljy);
        yyjy = (RadioButton) findViewById(R.id.opt_yyjy);
        tefy.setOnClickListener(buttonClick);
        tjjy.setOnClickListener(buttonClick);
        hljy.setOnClickListener(buttonClick);
        yyjy.setOnClickListener(buttonClick);
        
        tv1 = (ScrollView) findViewById(R.id.scr1);
		tv2 = (ScrollView) findViewById(R.id.scr2);
		tv3 = (ScrollView) findViewById(R.id.scr3);
		tv4 = (ScrollView) findViewById(R.id.scr4);
		tv1.setVisibility(View.VISIBLE);
		tv2.setVisibility(View.GONE);
		tv3.setVisibility(View.GONE);
		tv4.setVisibility(View.GONE);
        
        tabs = null;
		tabs = (TabHost)findViewById(R.id.tabhost);
		tabs.setup();
        tabs.setOnTabChangedListener(TabChangeListener);
        //设置Tab1
        footer_tab_btn1 = new ImageButton(this);
        footer_tab_btn1.setBackgroundResource(R.drawable.opt_hyzs);
        TabSpec tab1 = tabs.newTabSpec("opt_hyzs");  
        tab1.setIndicator(footer_tab_btn1);      // 设置tab1的名称
        tab1.setContent(R.id.include1);    // 关联控件  
        tabs.addTab(tab1);                // 添加tab1
        
        //产前需知
        footer_tab_btn2 = new ImageButton(this);
        footer_tab_btn2.setBackgroundResource(R.drawable.opt_cqxz);
        TabSpec tab2 = tabs.newTabSpec("opt_cqxz");  
        tab2.setIndicator(footer_tab_btn2);  
        tab2.setContent(R.id.include2);    
        tabs.addTab(tab2);                
        
        //产时妙用
        footer_tab_btn3 = new ImageButton(this);
        footer_tab_btn3.setBackgroundResource(R.drawable.opt_csmy);
        TabSpec tab3 = tabs.newTabSpec("opt_csmy");  
        tab3.setIndicator(footer_tab_btn3);        
        tab3.setContent(R.id.include3);
        tabs.addTab(tab3);                

        //好孕工具 
        footer_tab_btn4 = new ImageButton(this);
        footer_tab_btn4.setBackgroundResource(R.drawable.opt_hygj);
        TabSpec tab4 = tabs.newTabSpec("opt_hygj");  
        tab4.setIndicator(footer_tab_btn4);        
        tab4.setContent(R.id.include4);   
        tabs.addTab(tab4);    
        
        // 设置Tab5
        footer_tab_btn5 = new ImageButton(this);
        footer_tab_btn5.setBackgroundResource(R.drawable.opt_more);
        TabSpec tab5 = tabs.newTabSpec("opt_more");  
        tab5.setIndicator(footer_tab_btn5);        
        tab5.setContent(R.id.include5);     
        tabs.addTab(tab5);
        
        res =getResources();
        listView00 = (ListView) findViewById(R.id.ListView00);
        listView01 = (ListView) findViewById(R.id.ListView01);
        listView02 = (ListView) findViewById(R.id.ListView02);
        listView03 = (ListView) findViewById(R.id.ListView03);
        listView04 = (ListView) findViewById(R.id.ListView04);
        fetus = (ImageView) findViewById(R.id.fetus);	// 40周胎儿形状
        
        showView(week);		// 默认内容显示第几周
        left = (ImageButton) findViewById(R.id.left2);
        right = (ImageButton) findViewById(R.id.right2);
        left.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				checkWeek(false);
			}
		});
        right.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				checkWeek(true);
			}
		});
        
      //将GestureDetector绑定到LinearLayout
        detector=new GestureDetector(new GestureListener());  
        /*LinearLayout layout=(LinearLayout)findViewById(R.id.layout_hyzs);  
        layout.setOnTouchListener(new TouhListener());  
        layout.setLongClickable(true);*/
        
        fmzds = (ImageButton) findViewById(R.id.mm_fmzds);
        fmzds.setOnClickListener(buttonClick);
        
        changeUser = (ImageButton) findViewById(R.id.change);
        changeUser.setOnClickListener(buttonClick);
    }
    
    private void checkWeek(Boolean f){
    	if(f==false){
    		if(week<=1){
            	week=1;
            	left.setVisibility(View.INVISIBLE);
            	Toast.makeText(getApplicationContext(), "这是第一周", Toast.LENGTH_SHORT).show();
            	return;
            }
            week--;
    	}else if(f==true){
			if(week>=40){
				week=40;
				right.setVisibility(View.INVISIBLE);
            	Toast.makeText(getApplicationContext(), "这是最后一周", Toast.LENGTH_SHORT).show();
            	return;
            }
			week++;
    	}
    	showView(week);
    }
    private void showView(int w){
    	switch (w) {
		case 1:
			listView00.setAdapter(new ArrayAdapter<String>(MMMain.this, 
	                android.R.layout.simple_list_item_1, res.getStringArray(R.array.desc1)));
	        listView01.setAdapter(new ArrayAdapter<String>(MMMain.this, 
	                android.R.layout.simple_list_item_1, res.getStringArray(R.array.tefy1))); 
	        listView02.setAdapter(new ArrayAdapter<String>(MMMain.this, 
	                android.R.layout.simple_list_item_1, res.getStringArray(R.array.tjjy1))); 
	        listView03.setAdapter(new ArrayAdapter<String>(MMMain.this, 
	                android.R.layout.simple_list_item_1, res.getStringArray(R.array.hljy1))); 
	        listView04.setAdapter(new ArrayAdapter<String>(MMMain.this, 
	                android.R.layout.simple_list_item_1, res.getStringArray(R.array.yyjy1))); 
	        fetus.setImageResource(R.drawable.fetus_1);
			break;
		case 2:
			listView00.setAdapter(new ArrayAdapter<String>(MMMain.this, 
	                android.R.layout.simple_list_item_1, res.getStringArray(R.array.desc2)));
	        listView01.setAdapter(new ArrayAdapter<String>(MMMain.this, 
	                android.R.layout.simple_list_item_1, res.getStringArray(R.array.tefy2))); 
	        listView02.setAdapter(new ArrayAdapter<String>(MMMain.this, 
	                android.R.layout.simple_list_item_1, res.getStringArray(R.array.tjjy2))); 
	        listView03.setAdapter(new ArrayAdapter<String>(MMMain.this, 
	                android.R.layout.simple_list_item_1, res.getStringArray(R.array.hljy2))); 
	        listView04.setAdapter(new ArrayAdapter<String>(MMMain.this, 
	                android.R.layout.simple_list_item_1, res.getStringArray(R.array.yyjy2))); 
	        fetus.setImageResource(R.drawable.fetus_2);
	        break;
		case 3:
			listView00.setAdapter(new ArrayAdapter<String>(MMMain.this, 
	                android.R.layout.simple_list_item_1, res.getStringArray(R.array.desc3)));
	        listView01.setAdapter(new ArrayAdapter<String>(MMMain.this, 
	                android.R.layout.simple_list_item_1, res.getStringArray(R.array.tefy3))); 
	        listView02.setAdapter(new ArrayAdapter<String>(MMMain.this, 
	                android.R.layout.simple_list_item_1, res.getStringArray(R.array.tjjy3))); 
	        listView03.setAdapter(new ArrayAdapter<String>(MMMain.this, 
	                android.R.layout.simple_list_item_1, res.getStringArray(R.array.hljy3))); 
	        listView04.setAdapter(new ArrayAdapter<String>(MMMain.this, 
	                android.R.layout.simple_list_item_1, res.getStringArray(R.array.yyjy3))); 
	        fetus.setImageResource(R.drawable.fetus_3);
	        break;
		case 4:
			listView00.setAdapter(new ArrayAdapter<String>(MMMain.this, 
	                android.R.layout.simple_list_item_1, res.getStringArray(R.array.desc4)));
	        listView01.setAdapter(new ArrayAdapter<String>(MMMain.this, 
	                android.R.layout.simple_list_item_1, res.getStringArray(R.array.tefy4))); 
	        listView02.setAdapter(new ArrayAdapter<String>(MMMain.this, 
	                android.R.layout.simple_list_item_1, res.getStringArray(R.array.tjjy4))); 
	        listView03.setAdapter(new ArrayAdapter<String>(MMMain.this, 
	                android.R.layout.simple_list_item_1, res.getStringArray(R.array.hljy4))); 
	        listView04.setAdapter(new ArrayAdapter<String>(MMMain.this, 
	                android.R.layout.simple_list_item_1, res.getStringArray(R.array.yyjy4))); 
	        fetus.setImageResource(R.drawable.fetus_4);
	        break;
		}
    }
    private OnTabChangeListener TabChangeListener  = new OnTabChangeListener() {
		@Override
		public void onTabChanged(String tabId) {
			int j = tabs.getTabWidget().getTabCount();
			ImageButton currentView =(ImageButton) tabs.getCurrentTabView(); 
			for (int i = 0; i < j ; i++){
				if(tabs.getCurrentTab() == i){
					currentView.setEnabled(false);
				}else{
					if(tabs.getTabWidget().getChildTabViewAt(i) != null){
						((ImageButton)tabs.getTabWidget().getChildTabViewAt(i)).setEnabled(true);
					}
				}
			}
		}
	};;
	
	private OnClickListener buttonClick = new OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.setting:
				Intent setPage=new Intent();
				setPage.setClass(MMMain.this, Setting.class);
				startActivity(setPage);
				break;
			case R.id.mm_fmzds:
				break;
			case R.id.opt_tefy:
				tefy.setBackgroundResource(R.drawable.opt_tefyl);
				tjjy.setBackgroundResource(R.drawable.opt_tjjy);
				hljy.setBackgroundResource(R.drawable.opt_hljy);
				yyjy.setBackgroundResource(R.drawable.opt_yyjy);
				tv1.setVisibility(View.VISIBLE);
				tv2.setVisibility(View.GONE);
				tv3.setVisibility(View.GONE);
				tv4.setVisibility(View.GONE);
				break;
			case R.id.opt_tjjy:
				tjjy.setBackgroundResource(R.drawable.opt_tjjyl);
				tefy.setBackgroundResource(R.drawable.opt_tefy);
				hljy.setBackgroundResource(R.drawable.opt_hljy);
				yyjy.setBackgroundResource(R.drawable.opt_yyjy);
				tv1.setVisibility(View.GONE);
				tv2.setVisibility(View.VISIBLE);
				tv3.setVisibility(View.GONE);
				tv4.setVisibility(View.GONE);
				break;
			case R.id.opt_hljy:
				hljy.setBackgroundResource(R.drawable.opt_hljyl);
				tefy.setBackgroundResource(R.drawable.opt_tefy);
				tjjy.setBackgroundResource(R.drawable.opt_tjjy);
				yyjy.setBackgroundResource(R.drawable.opt_yyjy);
				tv1.setVisibility(View.GONE);
				tv2.setVisibility(View.GONE);
				tv3.setVisibility(View.VISIBLE);
				tv4.setVisibility(View.GONE);
				break;
			case R.id.opt_yyjy:
				yyjy.setBackgroundResource(R.drawable.opt_yyjyl);
				tefy.setBackgroundResource(R.drawable.opt_tefy);
				tjjy.setBackgroundResource(R.drawable.opt_tjjy);
				hljy.setBackgroundResource(R.drawable.opt_hljy);
				tv1.setVisibility(View.GONE);
				tv2.setVisibility(View.GONE);
				tv3.setVisibility(View.GONE);
				tv4.setVisibility(View.VISIBLE);
				break;
			case R.id.change:
				Intent bbLogin=new Intent();
				SharedPreferences sharedPreferences2 = getSharedPreferences("bbinfo", Context.MODE_PRIVATE);
				String bbname = sharedPreferences2.getString("bbname", "");
				// 判断是否存储宝宝的信息
				if(bbname!=null&&bbname!=""&&bbname.length()>0){
					bbLogin.setClass(MMMain.this, BBIndex.class);		// 跳转到宝宝首页
				}else{
					bbLogin.setClass(MMMain.this, BBLogin.class);		// 跳转到宝宝档案页面
				}
				startActivity(bbLogin);
				break;
			}
		}
	};
	
	//触摸屏幕监听  
    class TouhListener implements OnTouchListener{  
        @Override  
        public boolean onTouch(View v, MotionEvent event) {  
            return detector.onTouchEvent(event);  
        }  
    }  
    //手势滑动监听  
    class GestureListener implements OnGestureListener{  
  
        @Override  
        public boolean onDown(MotionEvent e) {  
        /*  
         * 当触摸屏幕是使用这个函数  
         */  
            return false;  
        }  
        @Override  
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,  
                float velocityY) {  
            // e1 触摸的起始位置，e2 触摸的结束位置，velocityX X轴每一秒移动的像素速度（大概这个意思） velocityY 就是Ｙ咯  
            //手势左,上为正 ——，右，下为负正  
            if (e2.getX()-e1.getX()>50) {  // 往右滑
                //为什么是50？ 这个根据你的模拟器大小来定，看看模拟器宽度，e2.getX()-e1.getX()<屏幕宽度就ＯＫ 
            	checkWeek(false);
                //要触发什么事件都在这里写就OK   
            } else if (e1.getX()-e2.getX()>50) {  	// 往左滑
				checkWeek(true);
            }  
            return false;  
        }  
  
        @Override  
        public void onLongPress(MotionEvent e) {  
          /*  
           * 长按事件  一切长事件按屏幕想要触发的事件都在这里写  
           */  
        }  
        @Override  
        public boolean onScroll(MotionEvent e1, MotionEvent e2,  
                float distanceX, float distanceY) {  
            return false;  
        }  
        @Override  
        public void onShowPress(MotionEvent e) {}  
        @Override  
        public boolean onSingleTapUp(MotionEvent e) {return false;}  
    }  
	
}